<?php
$auth = Auth::getAuth('current_user');
$db = new Database;
$fs = new functions;

if(isset($_POST['action'])){
	$date = $fs->currentDateTime();
	
	switch($_POST['action']){
		
		case "saveWorkflow":
			$workflow = $_POST['json_encode'];
			$json = json_decode($workflow, true);
			//DEACTIVATE OLD WORKFLOW
			$update_active = array('is_active' =>  '0');
			$where_active = array('form_id' =>  $json['form_id']);
			$db->update("tbworkflow",$update_active,$where_active);
			//INSERT WORKFLOW DETAILS
			$insert = array("title"	=>	$json['title'],
				"form_id"		=>	$json['form_id'],
				"description"	=>	$json['description'],
				"date"			=>	$date,
				"tbw_json"		=>	json_encode(array("width"=>$json['width'],"height"=>$json['height'])),
				"is_active"	=>	1
				);
			$workflow_id = $db->insert("tbworkflow",$insert);


			//UPDATE FORM STATUS(IS_ACTIVE) TO 1
			// $updateform_condition = array("id"=>$json['form_id']);
			// $updateform_set = array("is_active"=>"1");
			// $updateform_status = $db->update("tb_workspace",$updateform_set,$updateform_condition);


			//INSERT WORKFLOW OBJECTS
			foreach($json['nodes'] as $nodes){
				$insert_obects = array("workflow_id"	=>	$workflow_id,
								       "object_id"		=>	$nodes['node_data_id'],
								       "processorType" 	=>	$nodes['processorType'],
								       "processor" 		=>	$nodes['processor'],
								       "buttonStatus" 	=>	json_encode($nodes['buttonStatus']),
								       "condition_return" =>	json_encode($nodes['condition_return']),
								       "status"			=>	$nodes['status'],
								       "type_rel"		=>	$nodes['type_rel'],
								       "operator"		=>	$nodes['operator'],
								       "field"			=>	$nodes['field'],
								       "field_value"	=>	$nodes['field_value'],
								       "email"			=>	json_encode($nodes['workflow_email']),
								       "sms"			=>	json_encode($nodes['workflow_sms']),
								       "fieldEnabled"	=>	json_encode($nodes['fieldEnabled']),
								       "fieldRequired"	=>	json_encode($nodes['fieldRequired']),
								       "fieldHiddenValue"=>	json_encode($nodes['fieldHiddenValue']),
								       "json" 			=>	json_encode($nodes),
									);
				$db->insert("tbworkflow_objects",$insert_obects);
			}
			
			//INSERT WORKFLOW LINES
			foreach($json['lines'] as $lines){
				$insert_obects = array("workflow_id"	=>	$workflow_id,
								       "parent"			=>	$lines['parent'],
								       "child" 			=>	$lines['child'],
								       "json" 			=>	json_encode($lines),
									);
				$db->insert("tbworkflow_lines",$insert_obects);
			}
			$jsonReturn = array("title"=>$json['title'],"id"=>$workflow_id);

			//	echo "Your Workflow (" . $json['title'] . ") was successfully saved.";
			echo json_encode($jsonReturn);
		break;
		case "getProcessor":
			$value = $_POST['value'];
			$selectedValue = $_POST['selected'];
			$option = "<option value='0'>--Select--</option>";
			if($value=="1"){ /* FOR DEPARTMENT POSITION */
				$company_id = $auth['company_id'];
				$getOrgchart = $db->query("SELECT * FROM tborgchart WHERE company_id = '". $company_id ."' and is_active = '1'","numrows");
				if($getOrgchart>0){
					$option .= "<option value='1' ". functions::setSelected('1', $selectedValue) .">Head</option>";
					$option .= "<option value='2' ". functions::setSelected('2', $selectedValue) .">Assistant Head</option>";
				}else{
					$option = "0";
				}
			}else if($value=="2"){ /* FOR COMPANY POSITION */
				$query = "SELECT DISTINCT(position) FROM tbuser WHERE company_id = {$db->escape($auth['company_id'])} AND is_active = 1";
				$getPosition = $db->query($query,"array");
				foreach($getPosition as $position_array){
					$option .= "<option value='". $position_array['position'] ."' ". functions::setSelected($position_array['position'], $selectedValue) .">". $position_array['position'] ."</option>";
				}
			}else if($value=="3"){ /* FOR EMPLOYEE */
				$query = "SELECT id,first_name,middle_name,last_name FROM tbuser WHERE company_id = {$db->escape($auth['company_id'])} AND is_active = 1";
				$getUsers = $db->query($query,"array");
				foreach($getUsers as $users_array){
					$option .= "<option value='". $users_array['id'] ."' ". functions::setSelected($users_array['id'], $selectedValue) .">". $users_array['first_name']." ".$users_array['middle_name']." ". $users_array['last_name']."</option>";
				}
			}else if($value == "4"){
				$option = "<option value='1'>Requestor</option>";
			}else if($value == 5){
				$id = $_POST['form_id'];
				$fields_arr = functions::getFormFields($id);
				foreach($fields_arr as $fields){
					$option .= "<option value='". $fields ."' ". functions::setSelected($fields, $selectedValue) .">". $fields."</option>";
				}
			}
			echo $option;
		break;
		case "getFormFields":

			$active_fields = functions::getFormFields($_POST['form_id']);
	        $form_columns = array();
	        foreach ($active_fields as $value) {
	            array_push($form_columns, $value);
	        }
	        array_push($form_columns, "Status");
	        echo json_encode($form_columns);
	    break;
	}
	
}

?>